package com.example.aslongastoday.ui.backlog;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.aslongastoday.R;
import com.example.aslongastoday.adapter.RecycleAdapter;
import com.example.aslongastoday.adapter.ViewBackLogGroupAdapter;
import com.example.aslongastoday.bean.BeanFactory;
import com.example.aslongastoday.db.dao.BackLogDao;
import com.example.aslongastoday.db.dao.BackLogGroupDao;
import com.example.aslongastoday.domain.BackLog;

import java.util.List;

public class RecycleFragment extends Fragment  implements View.OnTouchListener{

    private ImageView iv_back;
    private TextView tv_title;
    private RecyclerView recyclerView;
    private RecycleAdapter recycleAdapter;

    public RecycleFragment() {
        // Required empty public constructor
    }

    public RecycleFragment(ViewBackLogGroupAdapter viewBackLogGroupAdapter) {

    }


    public static RecycleFragment newInstance(String param1, String param2) {
        RecycleFragment fragment = new RecycleFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View rootView = inflater.inflate(R.layout.fragment_recycle, container, false);
        //拦截touch请求, 防止泄露
        rootView.setOnTouchListener(this);

        //实例化控件
        iv_back = rootView.findViewById(R.id.iv_back);
        tv_title = rootView.findViewById(R.id.tv_title);

        tv_title.setText("回收站");

        //返回监听事件
        iv_back.setOnClickListener(new View.OnClickListener() {
            private FragmentManager fragmentManager;

            @Override
            public void onClick(View v) {
                //退出fragment
                ViewBackLogGroupAdapter viewBackLogGroupAdapter = (ViewBackLogGroupAdapter) BeanFactory.getBean("viewBackLogGroupAdapter");
                BackLogGroupDao dao = BackLogGroupDao.getInstance(getContext());
                viewBackLogGroupAdapter.setBackLogGroupList(dao.queryUnfinishedAll());
                viewBackLogGroupAdapter.notifyDataSetChanged();
                fragmentManager = getActivity().getSupportFragmentManager();
                fragmentManager.popBackStack();
            }
        });

        //数据库查找已完成的待办项
        BackLogDao backLogDao = BackLogDao.getInstance(rootView.getContext());
        List<BackLog> backLogList = backLogDao.queryAll(1);

        View listView = rootView.findViewById(R.id.backlog_list_rv);
        if(listView instanceof RecyclerView){
            recyclerView = (RecyclerView)listView;
            recycleAdapter = new RecycleAdapter(rootView.getContext(), backLogList);
            recyclerView.setLayoutManager(new LinearLayoutManager(rootView.getContext()));
            recyclerView.setAdapter(recycleAdapter);
        }

        return rootView;
    }

    // onTouch事件 将上层的触摸事件拦截
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return true;
    }
}